package ru.spbau.lifeincube.logic;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Montsev Mikhail
 *         Date: 12.05.13
 *         Time: 19:04
 */
public class Field {
    private Cell[][][] cells;
    private int sizeX = 0;
    private int sizeY = 0;
    private int sizeZ = 0;
    private StateStorage mStateStorage;

    public Field(int sizeX, int sizeY, int sizeZ) {
        this.cells = new Cell[sizeX][sizeY][sizeZ];
        for (int i = 0; i < sizeX; i++) {
            for (int j = 0; j < sizeY; j++) {
                for (int k = 0; k < sizeZ; k++)
                    cells[i][j][k] = new Cell(i, j, k, false);
            }
        }
        this.sizeX = sizeX;
        this.sizeY = sizeY;
        this.sizeZ = sizeZ;
    }

    private int getNeighborsCount(int x, int y, int z) {
        int count = 0;
        for (int i = x - 1; i < x + 2; i++) {
            for (int j = y - 1; j < y + 2; j++) {
                for (int k = z - 1; k < z + 2; k++) {
                    Cell cell = getCell(i, j, k);
                    if (cell != null)
                        if ((x != i || y != j || k != z) && cell.getState())
                            count++;
                }
            }
        }
        return count;
    }

    public int getNeighborsCount(Cell cell) {
        return getNeighborsCount(cell.getX(), cell.getY(), cell.getZ());
    }

    public Cell getCell(int x, int y, int z) {
        Cell cell = null;
        if (x >= 0 && x < sizeX && y >= 0 && y < sizeY && z >= 0 && z < sizeZ)
            cell = cells[x][y][z];
        return cell;
    }

    public int getSizeX() {
        return sizeX;
    }

    public int getSizeY() {
        return sizeY;
    }

    public int getSizeZ() {
        return sizeZ;
    }

    public List<Cell> setFieldState(int[][][] state) {
        List<Cell> added = new ArrayList<>();
        for (int i = 0; i < sizeX; i++) {
            for (int j = 0; j < sizeY; j++) {
                for (int k = 0; k < sizeZ; k++) {
                    if (state[i][j][k] > 0) {
                        cells[i][j][k].setState(true);
                        added.add(cells[i][j][k]);
                    }
                }
            }
        }
        return added;
    }

    public void setCellState(Cell cell, boolean state) {
        if (cell != null)
            cells[cell.getX()][cell.getY()][cell.getZ()].setState(state);
    }

    public void changeCellState(Cell cell) {
        if (cell != null)
            cells[cell.getX()][cell.getY()][cell.getZ()].setState(!cell.getState());
    }

    public void printField() {
        for (int i = 0; i < sizeX; i++) {
            for (int j = 0; j < sizeY; j++) {
                for (int k = 0; k < sizeZ; k++)
                    System.out.println(" " + (cells[i][j][k].getState() ? 1 : 0) + " ");
                System.out.println("");
            }
            System.out.println("");
        }
    }

}
